<template>
    <div class="app-container" style="padding:20px">
        <el-card>
            <FormTopBar :title="title" :flowInsId="form.flowInsId" :taskId="taskId" :businessId="form.id" :billType="form.billType" @submitForm="submitForm"></FormTopBar>
        </el-card>
        <FlowInfoCard  :id="form.id+''" :billType="form.billType" :flowInsId="form.flowInsId">
            <!-- 添加或修改员工入职对话框 -->
            <el-form ref="form" :model="form" :rules="rules" label-width="120px" :disabled="readonly">
                <el-card class="top15">
                    <div slot="header" class="clearfix">
                        <span>基础信息</span>
                        <span style="float: right" v-if="form.id!=undefined">单据编号：{{form.billCode}}</span>
                    </div>
                    <el-row class="mb8">
                        <el-alert title="时间不为整天计算方式：时间不足12小时算半天，时间超过12小时算一天。（请假天数可以根据实际情况进行手动调整）" type="success" :closable="false" style="padding: 20px"></el-alert>
                    </el-row>
                    <el-row>
                        <el-col :xs="24" :sm="12" :md="8" :lg="8" v-if="form.billType=='duty_leave'">
                            <el-form-item label="请假类型" prop="leaveType">
                                <el-select v-model="form.leaveType" placeholder="请选择请假类型" clearable>
                                    <el-option v-for="dict in dict.type.duty_leave_type" :key="dict.value" :label="dict.label" :value="dict.value"/>
                                </el-select>
                            </el-form-item>
                        </el-col>

                        <el-col :xs="24" :sm="12" :md="8" :lg="8">
                            <el-form-item label="起止时间" prop="startTime">
                                <el-date-picker v-model="timeRange" type="datetimerange" align="right"
                                                value-format="yyyy-MM-dd HH:mm:ss"
                                                start-placeholder="开始时间" end-placeholder="结束时间" @change="getDifDay"></el-date-picker>
                            </el-form-item>
                        </el-col>

                        <el-col :xs="24" :sm="12" :md="8" :lg="8">
                            <el-form-item label="天数" prop="days">
                                <el-input v-model="form.days" placeholder="请输入天数" />
                            </el-form-item>
                        </el-col>

                        <el-col :xs="24" :sm="12" :md="8" :lg="8"  v-if="form.billType=='duty_out'||form.billType=='duty_otherCityWork'">
                            <el-form-item label="外出地点" prop="destination" v-if="form.billType=='duty_out'">
                                <el-input v-model="form.destination" placeholder="请输入外出地点" />
                            </el-form-item>
                            <el-form-item label="出差地点" prop="destination" v-if="form.billType=='duty_otherCityWork'">
                                <el-input v-model="form.destination" placeholder="请输入出差地点" />
                            </el-form-item>
                        </el-col>

                    </el-row>
                    <el-row>
                        <el-form-item label="事由" prop="reason">
                            <el-input v-model="form.reason" type="textarea" :rows="5" placeholder="请输入事由"/>
                        </el-form-item>
                    </el-row>
                </el-card>
            </el-form>
        </FlowInfoCard>
    </div>
</template>

<script>
import FlowInfo from "@/views/workflow/flow/flowInfo";
import FlowInfoCard from "@/views/workflow/common/flowinfoCard";
import {addDutyApply, getDutyApply, updateDutyApply} from "@/api/oa/dutyApply"
import FormTopBar from "@/views/workflow/common/formTopBar"

export default {
    name: "DutyApplyForm",
    dicts: ['duty_leave_type'],
    components:{FlowInfo,FormTopBar,FlowInfoCard},
    data() {
        return {
            title:"",
            // 表单参数
            form: {
                days:0
            },
            timeRange:[],
            showFlowInfo:false,
            taskId:'',
            readonly:false,
            // 表单校验
            rules: {
                timeRange: [
                    {required: true, message: "起止时间不能为空", trigger: "blur"}
                ],
                leaveType: [
                    {required: true, message: "请假类型不能为空", trigger: "blur"}
                ],
            },
            showCommitDialog:false,
            commitType:""
        };
    },
    provide(){
        return {
            addCcedList:this.addCcedList
        }
    },
    created() {
        //单据的id主键
        const formId = this.$route.params && this.$route.params.formId;
        //单据的类型
        const billType = this.$route.params && this.$route.params.billType;
        const taskId = this.$route.query && this.$route.query.taskId;
        const readonly = this.$route.query && this.$route.query.readonly;
        if(billType){
            this.form.billType = billType;
            this.setTitle(billType);
        }
        if(taskId){
            this.taskId = taskId;
        }
        if("1"==readonly){
            this.readonly = true;
        }
        if(formId){
            this.form.id = formId;
            this.getDetailData(formId);
        }
    },
    mounted() {
    },
    methods: {
        //  获取单据信息
        getDetailData(id) {
            getDutyApply(id).then(response => {
                this.form = response.data;
                console.log(this.form)
                this.setTitle(this.form.billType);
                this.timeRange = [response.data.startTime,response.data.endTime]
            });
        },
        setTitle(billType){
            if(billType == "duty_leave"){
                this.title="请假申请单";
            }else if(billType == "duty_rest"){
                this.title="调休申请单";
            }else if(billType == "duty_out"){
                this.title="外出申请单";
            }else if(billType == "duty_overtime"){
                this.title="加班申请单";
            }else if(billType == "duty_otherCityWork"){
                this.title="出差申请单";
            }
        },
        /** 提交按钮 */
        submitForm() {
            console.log(this.timeRange)
            if(this.timeRange && this.timeRange.length>0){
                this.form.startTime = this.timeRange[0];
                this.form.endTime = this.timeRange[1];
            }

            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateDutyApply(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.closeThisPage();
                        });
                    } else {
                        addDutyApply(this.form).then(response => {
                            this.$modal.msgSuccess("保存成功");
                            this.closeThisPage();
                        });
                    }
                }
            });
        },
        getDifDay(value){
            console.log(value)
            if(value && value.length>0){
                let start = Date.parse(value[0]);
                let end = Date.parse(value[1]);
                let subTime = (end - start) / 1000;    //计算时间差,并将毫秒转化为秒
                let days = parseInt(subTime / 86400);  //天  24*60*60*1000
                let hours = parseInt(subTime / 3600) - (24 * (days));   //小时  60*60  总小时数-过去小时数=现在小时数
                // var mins = parseInt(subtime % 3600 / 60);    //分钟 - (day*24)  以60秒为一整份  取余 剩下秒数 秒数/60就是分钟数
                // var secs = parseInt(subtime % 60);   //以60秒为一整份  取余  剩下秒数
                console.log("天数："+days);
                console.log("小时："+hours);
                if(hours>0){
                    if(hours<=12){
                        days = days + 0.5;
                    }else{
                        days = days + 1;
                    }
                }else{
                    days = days + 1;
                }
                setTimeout(()=>{
                    this.form.days = days;
                },100)

            }
        },
        getDiffDay(date_1, date_2){
            let totalDays, diffDate
            let myDate_1 = Date.parse(date_1)
            let myDate_2 = Date.parse(date_2)
            diffDate = Math.abs(myDate_1 - myDate_2)
            totalDays = Math.floor(diffDate / (1000 * 3600 * 24))
            return totalDays
        },
        getDiffTime(new_date,old_date){
            var subtime = (new_date - old_date) / 1000;    //计算时间差,并将毫秒转化为秒
            var days = parseInt(subtime / 86400);  //天  24*60*60*1000
            var hours = parseInt(subtime / 3600) - 24 * days;   //小时  60*60  总小时数-过去小时数=现在小时数
            var mins = parseInt(subtime % 3600 / 60);    //分钟 - (day*24)  以60秒为一整份  取余 剩下秒数 秒数/60就是分钟数
            var secs = parseInt(subtime % 60);   //以60秒为一整份  取余  剩下秒数
            return "时间差是：" + days + "天 " + hours + "小时 " + mins + "分钟 " + secs + "秒 ";
        },
        closeThisPage(){
            let s = "";
            if (this.form.billType == "duty_leave")
                s = "/commonApply/duty/dutyLeaveApply";
            else if (this.form.billType == "duty_overtime")
                s = "/commonApply/duty/dutyOvertimeApply";
            else if (this.form.billType == "duty_rest")
                s = "/commonApply/duty/dutyRestApply";
            else if (this.form.billType == "duty_otherCityWork")
                s = "/commonApply/duty/dutyOtherCityWorkApply";
            else if (this.form.billType == "duty_out")
                s = "/commonApply/duty/dutyOutApply";

            this.$tab.closeOpenPage({ path: s,query: {billType:this.form.billType}});
        },
        // 创建单据时候，选择了抄送人
        addCcedList(ccedList){
            this.form.ccedList = ccedList;
            console.log("回填抄送人")
            console.log(this.form)
        }
    }
};
</script>
